package com.tyl.demo01;

import java.util.concurrent.TimeUnit;

public class SaleTicketDemo01 {
    public static void main(String[] args) {
        //并发：多个线程同时操作一个资源。把资源丢入线程中即可
        Ticket ticket=new Ticket();

        // new Thread(()->{ticket.sale();}).start();lombad表达式(参数)->{代码}

        new Thread(()->{
            try {
                TimeUnit.MILLISECONDS.sleep(3);
                for (int i = 0; i < 60; i++) {
                    ticket.sale();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();

        new Thread(()->{
            try {
                TimeUnit.MILLISECONDS.sleep(5);
                for (int i = 0; i < 60; i++) {
                    ticket.sale();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 60; i++) {
                ticket.sale();
            }
        },"C").start();
    }
}

class Ticket{
    private int number=50;
    //卖票
    public synchronized void sale(){
        if(number>0){
            System.out.println(Thread.currentThread().getName()+"卖出了"+number--+"张票"+"剩余"+number+"张票");
        }
    }
}
